<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>购物车</title>
    <link rel="stylesheet" type="text/css" href="./cart.css">
    <!-- <script src="vue.js"></script> -->
</head>

<body>
    <!--购物车商品table-->
    <div id="app">
        <div class="shoppingBar">
            <h2>购物清单</h2>
            <table>
                <tbody>
                    <tr>
                        <th><input type="checkbox" title="全选"></th>
                        <th>商品名称</th>
                        <th>数量</th>
                        <th>单价（元）</th>
                        <th>金额（元）</th>
                        <th>操作</th>
                    </tr>
                    <tr class="commodity">
                        <th><input type="checkbox" class="input" value="1"></th>
                        <th class="content"><img src="" width="100px" height="100px">
                            <p> </p>
                        </th>
                        <th><button type="button">
                                -
                            </button> <span>

                            </span> <button type="button">
                                +
                            </button></th>
                        <th>

                        </th>
                        <th class="money">

                        </th>
                        <th><span class="glyphicon glyphicon-remove">删除
                            </span></th>
                    </tr>
                    <tr class="commodity">
                        <th><input type="checkbox" class="input" value="2"></th>
                        <th class="content"><img src="" width="100px" height="100px">
                            <p> </p>
                        </th>
                        <th><button type="button">
                                -
                            </button> <span>

                            </span> <button type="button">
                                +
                            </button></th>
                        <th>

                        </th>
                        <th class="money">

                        </th>
                        <th><span class="glyphicon glyphicon-remove">删除
                            </span></th>
                    </tr>
                    <tr class="commodity">
                        <th><input type="checkbox" class="input" value="3"></th>
                        <th class="content"><img src="" width="100px" height="100px">
                            <p> </p>
                        </th>
                        <th><button type="button">
                                -
                            </button> <span>
                                1
                            </span> <button type="button">
                                +
                            </button></th>
                        <th>

                        </th>
                        <th class="money">

                        </th>
                        <th><span class="glyphicon glyphicon-remove">删除
                            </span></th>
                    </tr>
                </tbody>
            </table>
            <div class="shoppingBar-footer">
                <div class="manage"><span class="delAll">
                        删除所选商品
                    </span> <span class="return">
                        继续购物
                    </span></div> <button id="go" type="button">
                    去结算
                </button>
                <div class="buy"><span>
                        0
                    </span>
                    件商品总计:
                    <span>
                        ￥0
                    </span>
                </div>
            </div>
        </div>
    </div>

    <script>
        let goodslist = [
            { id: 1, title: '电视机', img: './images/托尔0.jpg', number: 1, price: 200 },
            { id: 2, title: '洗衣机', img: './images/托尔1.jpg', number: 1, price: 150 },
            { id: 3, title: '冰箱', img: './images/托尔2.jpg', number: 1, price: 100 }
        ]
        let index = 0;// 商品索引
        // 总计数量和金额
        let numberTotal = document.querySelector('.buy span:first-of-type')
        let moneyTotal = document.querySelector('.buy span:last-of-type')
        // 全选 单选
        let checkAll = document.querySelector('input')
        let checkList = document.querySelectorAll('.input')
        // 商品数量加减按钮
        let btnSubList = document.querySelectorAll('.commodity th:nth-of-type(3) button:first-of-type')
        let btnAddList = document.querySelectorAll('.commodity th:nth-of-type(3) button:last-of-type')
        // 删除所选商品 
        let delAll = document.querySelector('.delAll')
        // 渲染页面
        function init() {
            // 页面信息
            let title = document.querySelectorAll('.content p')
            let img = document.querySelectorAll('.content img')
            let number = document.querySelectorAll('.commodity th:nth-of-type(3) span')
            let price = document.querySelectorAll('.commodity th:nth-of-type(4)')
            let money = document.querySelectorAll('.commodity th:nth-of-type(5)')
            let numberTemp = 0, moneyTemp = 0
            goodslist.forEach((elem, i) => {
                title[i].innerHTML = elem.title
                img[i].src = elem.img;
                number[i].innerHTML = elem.number
                price[i].innerHTML = elem.price
                money[i].innerHTML = elem.price * elem.number
                numberTemp += elem.number
                moneyTemp += elem.price * elem.number
            });
            numberTotal.innerHTML = numberTemp
            moneyTotal.innerHTML = moneyTemp
        }
        init()

        // 全选
        checkAll.addEventListener('click', () => {
            if (checkAll.checked)
                checkList.forEach(ele => ele.checked = true);
            else
                checkList.forEach(ele => ele.checked = false);
        })

        // 单选
        checkList.forEach(ele => {
            ele.onclick = () => {
                let count = 0;
                checkList.forEach(ele => { if (ele.checked) count++ })
                checkAll.checked = count == checkList.length ? true : false
            }
        })

        // 添加数量
        btnAddList.forEach(ele => {
            ele.onclick = () => {
                index = [...btnAddList].indexOf(ele)
                goodslist[index].number++;
                init()
            }
        })

        // 减少数量
        btnSubList.forEach(ele => {
            ele.onclick = () => {
                index = [...btnSubList].indexOf(ele)
                if (goodslist[index].number > 1)
                    goodslist[index].number--;
                else
                    alert('数量最少一件')
                init()
            }
        })

        // 单间商品删除按钮
        let removeList = document.querySelectorAll('.glyphicon')
        removeList.forEach(ele => {
            ele.onclick = () => {
                let removeList = document.querySelectorAll('.glyphicon')
                index = [...removeList].indexOf(ele)
                console.log(index);
                goodslist.splice(index, 1);
                ele.parentElement.parentElement.parentElement.removeChild(ele.parentElement.parentElement)
                init()
            }
        });

        // 删除所选商品 
        delAll.onclick = () => {
            checkList.forEach((ele, i) => {
                if (ele.checked) {
                    index = [...checkList].indexOf(ele)
                    goodslist.splice(index - i, 1)
                    ele.parentElement.parentElement.parentElement.removeChild(ele.parentElement.parentElement)
                }
                init()
            });
        }
    </script>
</body>

</html>